#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2024/10/25 10:58
# @Author  : Tom_zc
# @FileName: upload_file.py
# @Software: PyCharm
import os
import click
import logging
import sys
from kafka import KafkaProducer
import ssl

logging.basicConfig(level=logging.INFO,
                    format="%(asctime)s  %(levelname)s:%(message)s")


def get_version_url(tag):
    return os.getenv('version_url') + tag


@click.command()
@click.option("--tag", help='The progress of step')
def main(tag):
    logging.info("=" * 20 + "start to send message" + "=" * 20)
    if not all([tag]):
        logging.info("Lack of params, Please check.")
        sys.exit(-1)

    context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
    context.verify_mode = ssl.CERT_NONE
    producer = KafkaProducer(bootstrap_servers=os.getenv('bootstrap_servers'),
                             sasl_mechanism="PLAIN",
                             ssl_context=context,
                             security_protocol='SASL_SSL',
                             sasl_plain_username=os.getenv('sasl_username'),
                             sasl_plain_password=os.getenv('sasl_password'))

    data = bytes(f'{{"Version": "{tag}", "Url": "{get_version_url(tag)}"}}', encoding="utf-8")
    producer.send(os.getenv('topic_name'), data)
    producer.close()
    logging.info("=" * 20 + "end to send message" + "=" * 20)


if __name__ == '__main__':
    main()
